sink("Peterson_Swartz.log")
library(readr)
library(ggplot2)

study1 <- read_csv("study1.csv")


###data cleaning

#Age
study1$Year<-2019
study1$Age_3 <- study1$Year - as.numeric(study1$Age_1)


#Gender
study1$Gender[study1$Gender==1]<-0
study1$Gender[study1$Gender==2]<-1
study1$Gender[study1$Gender==4]<-0

#Race
study1$Race_1[study1$Race_1==(2)]<-0
study1$Race_1[study1$Race_1==(3)]<-0
study1$Race_1[study1$Race_1==(4)]<-0
study1$Race_1[study1$Race_1==(5)]<-0
study1$Race_1[study1$Race_1==(6)]<-0
study1$Race_1[study1$Race_1==(7)]<-0

#Education
study1$Education[study1$Education==1]<-0
study1$Education[study1$Education==2]<-0
study1$Education[study1$Education==3]<-1
study1$Education[study1$Education==4]<-1
study1$Education[study1$Education==5]<-2
study1$Education[study1$Education==6]<-2
study1$Education[study1$Education==7]<-2
study1$Education[study1$Education==8]<-2
study1$Education[study1$Education==9]<-2
study1$Education[study1$Education==10]<-NA
study1$Education[study1$Education==11]<-NA


#PID
study1$PID[study1$PID==1]<-0
study1$PID[study1$PID==2]<-6
study1$PID[study1$PID==4]<-3

#Pid_dem
study1$pid_dem[study1$pid_dem==1]<-0
study1$pid_dem[study1$pid_dem==2]<-1

#pid_rep
study1$pid_rep[study1$pid_rep==1]<-6
study1$pid_rep[study1$pid_rep==2]<-5

#pid_ind
study1$pid_ind[study1$pid_ind==1]<-4
study1$pid_ind[study1$pid_ind==2]<-3
study1$pid_ind[study1$pid_ind==3]<-2

#pidfinal
study1$pidfinal<-as.numeric(study1$pid_ind)+as.numeric(study1$pid_dem)+as.numeric(study1$pid_rep)

# SEXISM SCALE
study1$Q85_1[study1$Q85_1==5]<-2
study1$Q85_1[study1$Q85_1==6]<-3
study1$Q85_1[study1$Q85_1==8]<-4
study1$Q85_1[study1$Q85_1==9]<-5
study1$Q85_1[study1$Q85_1==10]<-6

study1$Q85_2[study1$Q85_2==5]<-2
study1$Q85_2[study1$Q85_2==6]<-3
study1$Q85_2[study1$Q85_2==8]<-4
study1$Q85_2[study1$Q85_2==9]<-5
study1$Q85_2[study1$Q85_2==10]<-6

study1$Q85_3[study1$Q85_3==5]<-2
study1$Q85_3[study1$Q85_3==6]<-3
study1$Q85_3[study1$Q85_3==8]<-4
study1$Q85_3[study1$Q85_3==9]<-5
study1$Q85_3[study1$Q85_3==10]<-6

study1$Q85_4[study1$Q85_4==5]<-2
study1$Q85_4[study1$Q85_4==6]<-3
study1$Q85_4[study1$Q85_4==8]<-4
study1$Q85_4[study1$Q85_4==9]<-5
study1$Q85_4[study1$Q85_4==10]<-6

study1$Q85_5[study1$Q85_5==5]<-2
study1$Q85_5[study1$Q85_5==6]<-3
study1$Q85_5[study1$Q85_5==8]<-4
study1$Q85_5[study1$Q85_5==9]<-5
study1$Q85_5[study1$Q85_5==10]<-6

study1$Q85_6[study1$Q85_6==5]<-2
study1$Q85_6[study1$Q85_6==6]<-3
study1$Q85_6[study1$Q85_6==8]<-4
study1$Q85_6[study1$Q85_6==9]<-5
study1$Q85_6[study1$Q85_6==10]<-6




#Q98
study1$Q98_1[study1$Q98_1==5]<-2
study1$Q98_1[study1$Q98_1==6]<-3
study1$Q98_1[study1$Q98_1==8]<-4
study1$Q98_1[study1$Q98_1==9]<-5
study1$Q98_1[study1$Q98_1==10]<-6

study1$Q98_2[study1$Q98_2==5]<-2
study1$Q98_2[study1$Q98_2==6]<-3
study1$Q98_2[study1$Q98_2==8]<-4
study1$Q98_2[study1$Q98_2==9]<-5
study1$Q98_2[study1$Q98_2==10]<-6

study1$Q98_3[study1$Q98_3==5]<-2
study1$Q98_3[study1$Q98_3==6]<-3
study1$Q98_3[study1$Q98_3==8]<-4
study1$Q98_3[study1$Q98_3==9]<-5
study1$Q98_3[study1$Q98_3==10]<-6

study1$Q98_4[study1$Q98_4==5]<-2
study1$Q98_4[study1$Q98_4==6]<-3
study1$Q98_4[study1$Q98_4==8]<-4
study1$Q98_4[study1$Q98_4==9]<-5
study1$Q98_4[study1$Q98_4==10]<-6

study1$Q98_5[study1$Q98_5==5]<-2
study1$Q98_5[study1$Q98_5==6]<-3
study1$Q98_5[study1$Q98_5==8]<-4
study1$Q98_5[study1$Q98_5==9]<-5
study1$Q98_5[study1$Q98_5==10]<-6

## build sexism measures
study1$hostile_sexism<-study1$Q85_3+study1$Q85_6+study1$Q98_1+
  study1$Q98_2+study1$Q98_3

study1$ben_sexism<-study1$Q85_1+study1$Q85_2+study1$Q85_4+
  study1$Q85_5+study1$Q98_4+study1$Q98_5

study1$ben_men<-study1$Q90_1+study1$Q90_3+study1$Q90_4+
  study1$Q99_1+study1$Q99_3

study1$hostil_men<-study1$Q90_2+study1$Q90_5+study1$Q90_6+
  study1$Q99_2+study1$Q99_4+study1$Q99_5

#Code all Missing as 0
study1$Q9[is.na(study1$Q9)]<-0
study1$Q10[is.na(study1$Q10)]<-0
study1$Q11[is.na(study1$Q11)]<-0
study1$Q15[is.na(study1$Q15)]<-0
study1$Q16[is.na(study1$Q16)]<-0
study1$Q17[is.na(study1$Q17)]<-0

study1$Q19[is.na(study1$Q19)]<-0
study1$Q20[is.na(study1$Q20)]<-0
study1$Q21[is.na(study1$Q21)]<-0
study1$Q22[is.na(study1$Q22)]<-0
study1$Q23[is.na(study1$Q23)]<-0
study1$Q24[is.na(study1$Q24)]<-0
study1$Q25[is.na(study1$Q25)]<-0
study1$Q26[is.na(study1$Q26)]<-0
study1$Q36[is.na(study1$Q36)]<-0
study1$Q37[is.na(study1$Q37)]<-0
study1$Q38[is.na(study1$Q38)]<-0
study1$Q39[is.na(study1$Q39)]<-0
study1$Q13[is.na(study1$Q13)]<-0
study1$Q14[is.na(study1$Q14)]<-0


#SeparateTreatments 
study1$Independence<-as.numeric(study1$Q9)+as.numeric(study1$Q23)+as.numeric(study1$Q36)+as.numeric(study1$Q13)+as.numeric(study1$Q19)
study1$Obedience<-as.numeric(study1$Q10)+as.numeric(study1$Q24)+as.numeric(study1$Q37)+as.numeric(study1$Q14)+as.numeric(study1$Q20)
study1$Curiosity<-as.numeric(study1$Q11)+as.numeric(study1$Q25)+as.numeric(study1$Q38)+as.numeric(study1$Q16)+as.numeric(study1$Q21)
study1$Considerate<-as.numeric(study1$Q15)+as.numeric(study1$Q26)+as.numeric(study1$Q39)+as.numeric(study1$Q17)+as.numeric(study1$Q22)



#Approve
study1$Approve<-4-study1$Approve
#Vote
study1$Vote[study1$Vote==2]<-0
study1$Vote[study1$Vote==3]<-NA
study1$Vote[study1$Vote==4]<-NA

#LGBTmarry
study1$LGBTmarry[study1$LGBTmarry==4]<-2
study1$LGBTmarry[study1$LGBTmarry==6]<-3

#UseofForce
study1$uof<-5-study1$`Use of Force`

#Imports
study1$imports[study1$imports==2]<-0

#Border Wall
study1$wall<-3-study1$`Border wall`

#Guns
study1$guns<-3-study1$guns  


#Splitting--make the treatment variable

#make the fixed/fluid items missing if zero
study1$Independence[study1$Independence==0]<-NA
study1$Obedience[study1$Obedience==0]<-NA
study1$Curiosity[study1$Curiosity==0]<-NA
study1$Considerate[study1$Considerate==0]<-NA

#rescale the sexism measures to zero-one
study1$ben_sexism<-study1$ben_sexism/36
study1$ben_men<-study1$ben_men/36
study1$hostil_men<-study1$hostil_men/30
study1$hostile_sexism<-study1$hostile_sexism/30


study1$auth <- (study1$Independence-1 + 2-study1$Obedience + 
                              study1$Curiosity-1 + study1$Considerate-1)/4

table(study1$treatment)
#Control <- subset(study1, treatment==3)
#Girl <- subset(study1, treatment==5)
#Boy <- subset(study1, treatment==2)

